import QtQuick 2.0
import QtCharts 2.1
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
//![1]
ChartView {
    id: chartView
    animationOptions: ChartView.NoAnimation
    theme: ChartView.ChartThemeDark
    property var speedArray:[]
    property var powerArray:[]
    ValueAxis {
        id: axisY_speed
        min: 0.0
        max: 100.0
        titleText:"速度[km/h]"
    }
    ValueAxis {
        id: axisY_power
        min: 0.0
        max: 100.0
        titleText:"功率[W]"
    }
    ValueAxis {
        id: axisX
        min: 0
        max: 100
    }

    LineSeries {
        id: lineSeries1
        name: "速度"
        axisX: axisX
        axisY: axisY_speed
//        useOpenGL: chartView.openGL
    }
    LineSeries {
        id: lineSeries2
        name: "功率"
        axisX: axisX
        axisYRight:axisY_power
//        useOpenGL: chartView.openGL
    }
    function updateChart(current_speed,current_power)
    {
        var  len = lineSeries1.count;
        if(len > 90){
              lineSeries1.removePoints(0,len);
              lineSeries2.removePoints(0,len);
              for(var i=0;i<len-1;i++)
              {
                  speedArray[i] = speedArray[i+1];
                  lineSeries1.append(i,speedArray[i]);

                  powerArray[i] = powerArray[i+1];
                  lineSeries2.append(i,powerArray[i]);
              }

              speedArray[len-1]=current_speed;
              lineSeries1.append(len-1,current_speed);

              powerArray[len-1]=current_power;
              lineSeries2.append(len-1,current_power);

        }
        else{
                lineSeries1.append(len-1,current_speed);
                speedArray.push(current_speed);

                lineSeries2.append(len-1,current_power);
                powerArray.push(current_power);
        }
        var speed_max = speedArray[0] + 1;
        var speed_min = speedArray[0] - 1;
        var power_max = powerArray[0] + 1;
        var power_min = powerArray[0] - 1;
        for(var i = 0;i < len;i++ )
        {
            if(speed_max < speedArray[i])
                speed_max = speedArray[i] + 1;
            else if(speed_min > speedArray[i])
                speed_min = speedArray[i] - 1;

            if(power_max < powerArray[i])
                power_max = powerArray[i] + 1;
            else if(power_min > powerArray[i])
                power_min = powerArray[i] - 1;
        }
        axisY_speed.max =100// speed_max;
        axisY_speed.min =-2// speed_min;
        axisY_power.max =100 // power_max;
        axisY_power.min = -2// power_min;
        console.info(speed_max,speed_min,power_max,power_min)
    }
//    Timer{
//        id:data_timer
//        running:true
//        interval: 200
//        repeat:true
//        triggeredOnStart: true
//        onTriggered:{
//            var s = Math.random() * 100;
//            var p = Math.random() * 250;
//            updateChart(s,p)
//        }

//    }
}
